#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2013-11-15

@author: iori

'''
import os
import argparse
from torrent import Torrent

def main():
    parser = argparse.ArgumentParser(description='bt 种子操作功能, 默认扫描当前目录中的种子文件')
    parser.add_argument('-p', '--private', type=int, default=0, help='-p1 or --private=1 to\
                                             set private tracker, 0 public')
    parser.add_argument('-t', '--trackers', nargs='*', default='', help='tracker列表，多个用空格分割')
    # add option without any argument, use action='store_true':
    parser.add_argument('-r', '--recrusive', action='store_true')
    # 参数给定默认值
    parser.add_argument('-x', '--prefix', nargs='?', default='-pub', help='指定新种子文件添加的后缀标识')
    ns = parser.parse_args()
    path = '.'
    if ns.recrusive:
        fs = []
    else:
        fs = [x for x in os.listdir(path) if x.upper().endswith('TORRENT')]
    torrent = Torrent()
    for f in fs:
        torrent.load(f)
        torrent.private = ns.private
        torrent.add_trackers(ns.trackers)
        nf = '{0}{1}{2}'.format(os.path.splitext(f)[0], ns.prefix, '.torrent') 
        torrent.dump(nf)
        
if __name__ == '__main__':
    main()
    
    